package com.itlong.cloud.property;

import com.itlong.cloud.POJO.VO.property.PropertyRushFeeMessageVO;
import com.itlong.cloud.iottp.IIotTpSmsServiceClient;
import com.itlong.cloud.project.property.IPropertyAccountServiceClient;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <desc>
 *      物业-账单管理定时发送催费短信定时任务
 * </desc>
 *
 * @createDate 2018/08/31
 */
@EnableScheduling // 启用定时任务
@RestController
public class PropertyRushFeeMessageTimeTask {

    private static Logger LOG = LoggerFactory.getLogger(PropertyRushFeeMessageTimeTask.class);

    @Autowired
    private IPropertyAccountServiceClient iPropertyAccountServiceClient;

    @Autowired
    private IIotTpSmsServiceClient iIotTpSmsServiceClient;

    /**
     * <desc>
     *      定时发送催费短信
     * </desc>
     *
     * @author wangzhi
     * @createDate 2018/08/31
     */
//    @Scheduled(cron = "0 * * * * ?") // 每分钟执行一次（用于测试）
    @Scheduled(cron = "0 0 8 * * ?") // 每天早上8点执行
    public void sendRushFeeMesTask() throws Exception{
        try {
//            LOG.info("【定时任务】定时发送催费短信开始");
            List<PropertyRushFeeMessageVO> propertyRushFeeMessageVOList = iPropertyAccountServiceClient.getRushFeeMessage();
            for (PropertyRushFeeMessageVO propertyRushFeeMessageVO : propertyRushFeeMessageVOList){
                String content;
                if (StringUtils.isNotBlank(propertyRushFeeMessageVO.getPenaltyPercentage()) && !propertyRushFeeMessageVO.getPenaltyPercentage().equals("0")) {
                    content = "尊敬的户主：贵户于" + propertyRushFeeMessageVO.getBillTime() + "的" + propertyRushFeeMessageVO.getBillName() + "为" + propertyRushFeeMessageVO.getPaidAmount() +
                            "元还未缴纳，请您于" + propertyRushFeeMessageVO.getDeadline() + "前缴纳，避免产生滞纳金，如您不便请致电物业上门收取或下载手机App【云半径】进行缴费，谢谢您的合作！(" +
                            propertyRushFeeMessageVO.getPropertyName() + ")";
                } else {
                    content = "尊敬的户主：贵户于" + propertyRushFeeMessageVO.getBillTime() + "的" + propertyRushFeeMessageVO.getBillName() + "为" + propertyRushFeeMessageVO.getPaidAmount() +
                            "元还未缴纳，如您不便请致电物业上门收取或下载手机App【云半径】进行缴费，谢谢您的合作！(" + propertyRushFeeMessageVO.getPropertyName() + ")";
                }
                iIotTpSmsServiceClient.sendSmsCodeByPhone(propertyRushFeeMessageVO.getPhoneNum(),content);
            }
//            LOG.info("【定时任务】定时发送催费短信成功");
        } catch (Exception e) {
            LOG.error("【定时任务】定时发送催费短信失败");
            throw e;
        }
    }
}
